package com.itheima.shengxiao2;

public class Foodie implements Runnable {
    private Desk desk ;

    public Foodie(Desk desk) {
        this.desk = desk;
    }

    @Override
    public void run() {

        //死循环
        while (true) {

            synchronized (desk.getObj()) {
                if (desk.getCount() == 0) {
                    break;
                }
                if (!desk.isFan()) { // 判断桌上是否有饭
                    //没有饭  等着
                    try {
                        desk.getObj().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    //有饭 吃饭
                    System.out.println("吃货正在吃一碗饭");
                    //更改桌子上饭的装态
                    desk.setFan(false);
                    //总量-1
                    desk.setCount(desk.getCount() - 1);
                    //叫厨子继续做
                    desk.getObj().notifyAll();
                }
            }
        }
    }
}
